using System;
using System.Net.Mail;
using System.Text;
using System.Web.Security;
using System.Xml;


namespace SpiderMaster.AspNet.TradingSystem
{
    public partial class EmailManagerOld
    {
        internal partial class Reader
        {
            internal static void LoadRegistraion(MailMessage EmailMessage, String UserName, string FirstName, string Password, string EmailAddress, Boolean RequireActivation)
            {
                DomainSettings CurrentDomainSettings = DomainSettings.GetSharedSettings();

                #region Load Template

                //load email body form xml file and set the constant values from xml 
                XmlDocument doc = new XmlDocument();
                doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\Email\Body\RegistrationEmail.xml");

                XmlElement el = doc.DocumentElement;
                XmlNode nodeSubject = el.ChildNodes[0].ChildNodes[0];
                XmlNode nodeBody = el.ChildNodes[1].ChildNodes[0];
                XmlNode nodefrom = el.ChildNodes[2].ChildNodes[0];
                XmlNode nodedisplay = el.ChildNodes[3].ChildNodes[0];
                XmlNode nodebcc = el.ChildNodes[4].ChildNodes[0];

                #endregion

                #region Set Site Email Address

                String EmailFrom;
                if (CurrentDomainSettings.DynamicEmailEnabled)
                    EmailFrom = nodefrom.Value.Replace("$SiteEmail$", CurrentDomainSettings.DynamicEmailAddress);
                else
                    EmailFrom = CurrentDomainSettings.SiteEmailAddress;

                #endregion

                EmailMessage.Subject = nodeSubject.Value.Replace("$SiteName$", CurrentDomainSettings.SiteName);
                EmailMessage.From = new MailAddress(EmailFrom, nodedisplay.Value.Replace("$SiteName$", CurrentDomainSettings.SiteName));
                EmailMessage.To.Clear();
                EmailMessage.To.Add(EmailAddress);

                string[] bccList = nodebcc.Value.Split(new char[] { ';' });
                EmailMessage.Bcc.Clear();
               
                foreach (string bccstr in bccList)
                    EmailMessage.Bcc.Add(bccstr);

                #region Response Link

                String ResponseLink;

                if (RequireActivation == true)
                {
                    ResponseLink = "<a href=http://" + CurrentDomainSettings.DomainName +
                       LinkManager.AppendUrl(VirtualPage.MemberEmailConfirmation, true).Replace("~/", "/") +
                      LinkManager.AppendUrl(QueryOperator.EmailGuid, Membership.GetUser(UserName).ProviderUserKey.ToString(), true) +
                      ">|&gt;&gt; Activate Now &lt;&lt;|</a>";
                }
                else ResponseLink = LinkLoader.Cache.LogInLink();

                #endregion

                #region Body Formatting

                StringBuilder body = new StringBuilder();
                body.Append("<html> <head> </head> <body> <p>");

                string EmailBody = nodeBody.Value;
                EmailBody = EmailBody.Replace("$SiteName$", CurrentDomainSettings.SiteName);
                EmailBody = EmailBody.Replace("$LoginHeader$", LinkLoader.Cache.LoginHeaderImageLink());
                EmailBody = EmailBody.Replace("$FirstName$", FirstName);
                EmailBody = EmailBody.Replace("$UserName$", UserName);
                EmailBody = EmailBody.Replace("$Password$", Password);
                EmailBody = EmailBody.Replace("$ResponseLink$", ResponseLink);
                EmailBody = EmailBody.Replace("$TradingCenterSupport$", LinkLoader.Cache.TradingCenterSupportLink());
                EmailBody = EmailBody.Replace("$TermsAndConditionsLink$", LinkLoader.Cache.TermsAndConditoinsLink());

                body.Append(EmailBody);
                body.Append("</p> </body></html>");

                EmailMessage.Body = body.ToString();

                #endregion
            }
        }
    }
}